package gemini.castor.ui.client.page.content.store.shoppingcart.shoppingcartview;

import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.content.store.shoppingcart.ShoppingCartEvent;
import gemini.castor.ui.client.page.content.store.shoppingcart.ShoppingCartItem;
import gemini.castor.ui.client.page.content.store.shoppingcart.cartcheckout.CartCheckOutObject;
import gemini.castor.ui.client.page.content.store.shoppingcart.cartsummary.ShoppingCartSummaryEvent;
import gemini.castor.ui.client.page.content.store.shoppingcart.cartsummary.ShoppingCartSummaryHandler;
import gemini.castor.ui.client.page.content.store.shoppingcart.cartsummary.ShoppingCartSummaryWidget;
import gemini.castor.ui.client.page.navigation.NavigationEvent;
import gemini.castor.ui.client.page.widget.dialog.GmnDialog;
import gemini.castor.ui.client.page.widget.dialog.GmnDialogEvent;
import gemini.castor.ui.client.page.widget.dialog.GmnDialogHandler;
import gemini.castor.ui.client.utils.PlaceConstants;
import gemini.castor.ui.client.utils.PlaceConstants.NavigationItem;

import java.util.ArrayList;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.inject.Inject;

public class ShoppingCartViewPresenter extends
		CastorWidgetPresenter<ShoppingCartViewPresenter.Display> {

	@Inject
	public ShoppingCartViewPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {
		void setObject(ShoppingCartViewObject object);
		void loadData();
		ShoppingCartSummaryWidget getOwgOrderSummary();
		HasClickHandlers getCancelCartClickHandlers();
		HasClickHandlers getAddMoreCartClickHandlers();
		String getConfirmMessage();
		ShoppingCartViewObject getObject();
		HasClickHandlers getCheckOutClickHandlers();
	}

	private ShoppingCartViewObject inputObject;
	private Object outputObject;

	public static final Place PLACE = new Place(PlaceConstants.PLACE_CATALOG);

	@Override
	protected void onBind() {
		registerHandler(display.getOwgOrderSummary().addItemsChangedHandler(new ShoppingCartSummaryHandler() {
			
			@Override
			public void onItemsChanged(ShoppingCartSummaryEvent event) {
				ShoppingCartEvent.ForwardType fwType = ShoppingCartEvent.ForwardType.UPDATE;
				if(event.getItem().isDeleted()){
					fwType = ShoppingCartEvent.ForwardType.REMOVE;
				}
				ShoppingCartEvent cartEvent = new ShoppingCartEvent(fwType);
				cartEvent.setItems(new ArrayList<ShoppingCartItem>(1));
				cartEvent.getItems().add(event.getItem());
				eventBus.fireEvent(cartEvent);

				display.loadData();
			}
		}));
		
		registerHandler(display.getCancelCartClickHandlers().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				confirmCancel();
			}
		}));
		
		registerHandler(display.getAddMoreCartClickHandlers().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						eventBus.fireEvent(new NavigationEvent(NavigationItem.PRODUCT_CATALOG));
					}
				}));
		
		registerHandler(display.getCheckOutClickHandlers().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						
						CartCheckOutObject checkOutObject = new CartCheckOutObject();
						checkOutObject.setPaymentValue((long) inputObject.getDiscountedRetail());
						checkOutObject.setDistributorCode(inputObject.getDistributorCode());
						checkOutObject.setFullName(inputObject.getFullName());
						outputObject = checkOutObject;
						
						localEventBus.fireEvent(new CartViewEvent());
					}
				}));

		initDisplay();
	}
	
	private void confirmCancel(){
		String message = display.getConfirmMessage();

		GmnDialog dlgConfirm = new GmnDialog("Confirm", message, "Yes", "No");

		dlgConfirm.addConfirmationHandler(new GmnDialogHandler() {

			@Override
			public void onOK(GmnDialogEvent event) {
				for (ShoppingCartItem it : display.getObject().getItems()) {
					it.setDeleted(true);
				}
				display.loadData();
			
				ShoppingCartEvent cartEvent = new ShoppingCartEvent(ShoppingCartEvent.ForwardType.REMOVE);
				cartEvent.setItems(display.getObject().getItems());
				eventBus.fireEvent(cartEvent);
			}

			@Override
			public void onCancel(GmnDialogEvent event) {
				
			}
		});

		dlgConfirm.show();
	}
	
	private void initDisplay(){
		display.setObject(inputObject);
		display.loadData();
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	public void setInputObject(Object ob) {
		inputObject = (ShoppingCartViewObject)ob;
	}

	@Override
	public Object getOutputObject() {
		return outputObject;
	}
}
